package com.ringtheweb.agi.services.impl;

import java.util.Date;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;

import org.jboss.annotation.ejb.LocalBinding;

import com.ringtheweb.agi.services.api.IPlayListCallManager;
import com.ringtheweb.agi.services.api.IPlayListCallScheduler;

/**
 * Implementation of {@link IPlayListCallScheduler}
 * @author ldewavri
 *
 */
@Stateless
@LocalBinding(jndiBinding="PlayListCallScheduler") 
public class PlayListCallScheduler implements IPlayListCallScheduler{
	
    /** Service used for scheduling tasks. */
    @Resource private TimerService timerService;
    
    @EJB private IPlayListCallManager playListManager;

    /**
     * {@inheritDoc}
     */
	public void schedule(Long playListId, Date date) {
		timerService.createTimer((date.getTime()-System.currentTimeMillis()), playListId);
	}
	
	/**
	 * 
	 * @param timer
	 */
	@Timeout
	public void handleTimeOut(Timer timer) {
		Long playListId = (Long)timer.getInfo();
		playListManager.play(playListId);
	}

}
